<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>digichef.voting.tests.tests</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="digichef-module.html">Package&nbsp;digichef</a> ::
        <a href="digichef.voting-module.html">Package&nbsp;voting</a> ::
        <a href="digichef.voting.tests-module.html">Package&nbsp;tests</a> ::
        Module&nbsp;tests
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="digichef.voting.tests.tests-module.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== MODULE DESCRIPTION ==================== -->
<h1 class="epydoc">Module tests</h1><p class="nomargin-top"><span class="codelink"><a href="digichef.voting.tests.tests-pysrc.html">source&nbsp;code</a></span></p>
<pre class="literalblock">

&gt;&gt;&gt; from django.contrib.auth.models import User
&gt;&gt;&gt; from voting.models import Vote
&gt;&gt;&gt; from voting.tests.models import Item

##########
# Voting #
##########

# Basic voting ###############################################################

&gt;&gt;&gt; i1 = Item.objects.create(name='test1')
&gt;&gt;&gt; users = []
&gt;&gt;&gt; for username in ['u1', 'u2', 'u3', 'u4']:
...     users.append(User.objects.create_user(username, '%s@test.com' % username, 'test'))
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 0, 'num_votes': 0}
&gt;&gt;&gt; Vote.objects.record_vote(i1, users[0], +1)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 1, 'num_votes': 1}
&gt;&gt;&gt; Vote.objects.record_vote(i1, users[0], -1)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': -1, 'num_votes': 1}
&gt;&gt;&gt; Vote.objects.record_vote(i1, users[0], 0)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 0, 'num_votes': 0}
&gt;&gt;&gt; for user in users:
...     Vote.objects.record_vote(i1, user, +1)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 4, 'num_votes': 4}
&gt;&gt;&gt; for user in users[:2]:
...     Vote.objects.record_vote(i1, user, 0)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 2, 'num_votes': 2}
&gt;&gt;&gt; for user in users[:2]:
...     Vote.objects.record_vote(i1, user, -1)
&gt;&gt;&gt; Vote.objects.get_score(i1)
{'score': 0, 'num_votes': 4}

&gt;&gt;&gt; Vote.objects.record_vote(i1, user, -2)
Traceback (most recent call last):
    ...
ValueError: Invalid vote (must be +1/0/-1)

# Retrieval of votes #########################################################

&gt;&gt;&gt; i2 = Item.objects.create(name='test2')
&gt;&gt;&gt; i3 = Item.objects.create(name='test3')
&gt;&gt;&gt; i4 = Item.objects.create(name='test4')
&gt;&gt;&gt; Vote.objects.record_vote(i2, users[0], +1)
&gt;&gt;&gt; Vote.objects.record_vote(i3, users[0], -1)
&gt;&gt;&gt; Vote.objects.record_vote(i4, users[0], 0)
&gt;&gt;&gt; vote = Vote.objects.get_for_user(i2, users[0])
&gt;&gt;&gt; (vote.vote, vote.is_upvote(), vote.is_downvote())
(1, True, False)
&gt;&gt;&gt; vote = Vote.objects.get_for_user(i3, users[0])
&gt;&gt;&gt; (vote.vote, vote.is_upvote(), vote.is_downvote())
(-1, False, True)
&gt;&gt;&gt; Vote.objects.get_for_user(i4, users[0]) is None
True

# In bulk
&gt;&gt;&gt; votes = Vote.objects.get_for_user_in_bulk([i1, i2, i3, i4], users[0])
&gt;&gt;&gt; [(id, vote.vote) for id, vote in votes.items()]
[(1, -1), (2, 1), (3, -1)]
&gt;&gt;&gt; Vote.objects.get_for_user_in_bulk([], users[0])
{}

&gt;&gt;&gt; for user in users[1:]:
...     Vote.objects.record_vote(i2, user, +1)
...     Vote.objects.record_vote(i3, user, +1)
...     Vote.objects.record_vote(i4, user, +1)
&gt;&gt;&gt; list(Vote.objects.get_top(Item))
[(&lt;Item: test2&gt;, 4), (&lt;Item: test4&gt;, 3), (&lt;Item: test3&gt;, 2)]
&gt;&gt;&gt; for user in users[1:]:
...     Vote.objects.record_vote(i2, user, -1)
...     Vote.objects.record_vote(i3, user, -1)
...     Vote.objects.record_vote(i4, user, -1)
&gt;&gt;&gt; list(Vote.objects.get_bottom(Item))
[(&lt;Item: test3&gt;, -4), (&lt;Item: test4&gt;, -3), (&lt;Item: test2&gt;, -2)]

&gt;&gt;&gt; Vote.objects.get_scores_in_bulk([i1, i2, i3, i4])
{1: {'score': 0, 'num_votes': 4}, 2: {'score': -2, 'num_votes': 4}, 3: {'score': -4, 'num_votes': 4}, 4: {'score': -3, 'num_votes': 3}}
&gt;&gt;&gt; Vote.objects.get_scores_in_bulk([])
{}

</pre>

<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Apr  1 02:28:20 2010
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
